﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SD.Business;
using SD.DataAccess;

namespace SD.Web.Iteration2.Directorate
{
    public partial class AddDirectorate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                HiddenFieldOrganizationId.Value = Request.QueryString["OrgID"].ToString();
            }
            AddressLookupControl1.SelectedValue += new EventHandler<Web.Controls.Lookup.SelectedValueEventArgs>(AddressLookupControl1_SelectedValue);
        }

        /// <summary>
        /// load data into dropdownlist of Nation Country
        /// </summary>
        void LoadData()
        {
            DropDownListNationCountry.DataSource = CountryBO.SelectCountries();
            DropDownListNationCountry.DataTextField = "CountryName";
            DropDownListNationCountry.DataValueField = "CountryId";
            DropDownListNationCountry.DataBind();
        }

        /// <summary>
        /// Select value event of addresslookupcontrol
        /// </summary>
        void AddressLookupControl1_SelectedValue(object sender, Controls.Lookup.SelectedValueEventArgs e)
        {
            HiddenFieldAddressId.Value = e.Address.AddressId.ToString();
            TextBoxPostcode.Text = e.Address.Postcode;
            TextBoxAddressLine1.Text = e.Address.AddressLine;
            TextBoxAddressLine2.Text = e.Address.AddressLine2;
            TextBoxAddressLine3.Text = e.Address.AddressLine3;
        }

        /// <summary>
        /// Add directorate
        /// </summary>
        void Add()
        {
            SD.DataAccess.Directorate newDirectorate = new SD.DataAccess.Directorate();
            newDirectorate.DirectorateName = TextBoxDirectorateName.Text;
            if (!string.IsNullOrEmpty(HiddenFieldBusinessId.Value))
            {
                newDirectorate.BusinessId = Convert.ToInt32(HiddenFieldBusinessId.Value);
            }
            /*else
            {
                newDirectorate.BusinessId = 0;
            }*/
            if (!string.IsNullOrEmpty(HiddenFieldAddressId.Value))
            {
                newDirectorate.AddressId = Convert.ToInt32(HiddenFieldAddressId.Value);
            }
            if (!string.IsNullOrEmpty(HiddenFieldContactId.Value))
            {
                newDirectorate.ContactId = Convert.ToInt32(HiddenFieldContactId.Value);
            }
            newDirectorate.ShortDescription = TextBoxShortDescription.Text;
            newDirectorate.FullDescription = TextBoxFullDescription.Text;
            newDirectorate.CityTown = TextBoxCityTown.Text;
            newDirectorate.County = TextBoxCounty.Text;
            newDirectorate.OrganizationId = int.Parse(Request.QueryString["OrgID"]);
            newDirectorate.CountryId = DropDownListNationCountry.SelectedItem.Value;
            newDirectorate.PhoneNumber = TextBoxPhoneNumber.Text;
            newDirectorate.Fax = TextBoxFax.Text;
            newDirectorate.Email = TextBoxEmail.Text;
            newDirectorate.WebAddress = TextBoxWebAddress.Text;
            newDirectorate.CharityNumber = TextBoxCharityNumber.Text;
            newDirectorate.CompanyNumber = TextBoxCompanyNumber.Text;
            if (LabelIsActive.Text == "Yes")
            {
                newDirectorate.IsActive = true;
            }
            else
            {
                newDirectorate.IsActive = false;
            }
            bool success = new DirectorateBUS().AddDirectorate(newDirectorate);
            //check update
            if (success == true)
            {
                LabelSuccess.Text = "Add successfully!";
            }
        }

        /// <summary>
        /// Button Save Click event
        /// </summary>
        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            if (new DirectorateBUS().CheckNameDirectorate(TextBoxDirectorateName.Text))
            {
                LabelNameExists.Text = "Directorate Name has already existed";
            }
            else
            {
                Add();
                LabelNameExists.Text = "";
            }
        }
       
        /// <summary>
        /// Button Back Click event. Redirect to the directorate list page
        /// </summary>
        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Views/Orgranization/OrganizationDetail.aspx?id=" + HiddenFieldOrganizationId.Value);
        }

        /// <summary>
        /// TypeOfBusinessLookup button click event. Open TypeOfBusinessLookup
        /// </summary>
        protected void ButtonTypeOfBusinessLookup_Click(object sender, EventArgs e)
        {
            TypeOfBusinessLookup1.Popup();
        }

        /// <summary>
        /// ContactLookup button click event. Open ContactLookup
        /// </summary>
        protected void ButtonContactLookup_Click(object sender, EventArgs e)
        {
            ContactLookup1.Popup();
        }

        /// <summary>
        /// PostcodeLookup button click event. Open AddressLookupControl
        /// </summary>
        protected void ButtonPostcodeLookup_Click(object sender, EventArgs e)
        {
            AddressLookupControl1.Popup();
        }
    }
}